﻿//import { debug } from "util";
"use strict";
define(['admin/app', "service/admin/chargeHtmlService"], function (app) {
    app.controller('chargeHtmlController', function ($scope, $stateParams, $state,$location, utils, chargeHtmlService) {
        $scope.query = { page: 1, size: 20, start_time: '', end_time: '', q: '', pay_channel: '', order_no: '', pay_type: '0', is_finish:'-1' };
        $scope.del_ids=[];
		$.extend($scope.query, $stateParams);
        var methods = {
            search: function () {
                chargeHtmlService.get_htmls($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            search_page:function(page){
            	$scope.query.page = page;
	            $location.search('page', $scope.query.page);
            },
            html_first:function(id){
            	$scope.psdisabled = true;
                chargeHtmlService.html_first({id:id}).success(function (response) {
                    utils.notify(response.data != null ? response.data:'', response.result_code == 1000 ? "success" : "warning");
                    if (response.result_code == 1000)
                        methods.search();
                    $scope.psdisabled = false;
                }).error(function () { $scope.psdisabled = false; });
            },
            html_del_choose:function(){
            	var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    $scope.psdisabled = true;
	                chargeHtmlService.html_del_choose($scope.query).success(function (response) {
	                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
	                    if (response.result_code == 1000)
                        	methods.search();
	                    $scope.psdisabled = false;
	                }).error(function () { $scope.psdisabled = false; });
                });
            },
            html_del:function(id){
            	var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    $scope.psdisabled = true;
	            	var del_ids=[];
	            	del_ids.push(id);
	                chargeHtmlService.html_dels(del_ids).success(function (response) {
	                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
	                    if (response.result_code == 1000)
                        	methods.search();
	                    $scope.psdisabled = false;
	                }).error(function () { $scope.psdisabled = false; });
                });
            },
            html_dels:function(){
           		var modal = utils.confirm({ msg: "是否需要删除当前项目", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
	            	$scope.psdisabled = true;
	                chargeHtmlService.html_dels($scope.del_ids).success(function (response) {
	                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
	                    if (response.result_code == 1000)
                        	methods.search();
		            	$scope.del_ids=[];
	                    $scope.psdisabled = false;
	                }).error(function () { $scope.del_ids=[]; $scope.psdisabled = false; });
				});
            },
            choose: function ($event, id) {
                var checkbox = $event.target;
                var checked = checkbox.checked;
                var name = checkbox.name;
                var idx = $scope.del_ids.indexOf(id);
                if (checked && idx == -1) {
                    $scope.del_ids.push(id);
                }
                else if (!checked && idx != -1) {
                    $scope.del_ids.splice(idx, 1);
                }
            },
            choose_all:function(){
            	if($scope.del_ids.length==$scope.query.size)
            		$scope.del_ids=[];
            	else{
	            	$('input[name=id]').each(function(){
	            		$scope.del_ids.push($(this).attr('ng-true-value'));
	            	});
            	}
            }
        }

        angular.extend($scope, methods);
		methods.search();
    });
});
